package com.ruoyi.monitor.service;

import com.github.pagehelper.PageHelper;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.monitor.dao.entity.CameraInfo;
import com.ruoyi.monitor.dao.mapper.CameraInfoMapper;
import com.ruoyi.monitor.dto.req.CameraInfoQueryReqDTO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @desc: 监控设备信息管理
 * @author：cc
 * @date: 2024/7/25
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class CameraInfoService {

    private final CameraInfoMapper cameraInfoMapper;

    /**
     * 根据序列号集合查询设备信息
     */
    public List<CameraInfo> queryBySerials(List<String> serials) {
        return cameraInfoMapper.selectBySerials(serials);
    }

    public List<CameraInfo> queryBySerial(String deviceSerial) {
        return cameraInfoMapper.selectBySerial(deviceSerial);
    }

    public List<CameraInfo> queryList(CameraInfoQueryReqDTO req, Pageable page) {
        if (page != null) {
            PageHelper.startPage(page.getPageNumber() + 1, page.getPageSize());
        }
        return cameraInfoMapper.selectList(req);
    }

    /**
     * 添加摄像头
     */
    public int create(CameraInfo cameraInfo) {
        cameraInfo.setCreateBy(SecurityUtils.getUsername());
        return cameraInfoMapper.insert(cameraInfo);
    }

    /**
     * 修改摄像头信息
     * 禁止修改所属序列号
     */
    public int update(CameraInfo cameraInfo) {
        cameraInfo.setDelFlag(null);
        cameraInfo.setDeviceSerial(null);
        cameraInfoMapper.updateSelective(cameraInfo);
        return 0;
    }

    /**
     * 修改原序列号为originalSerial的摄像头的序列号为newSerial
     */
    public void updateDeviceSerial(String originalSerial, String newSerial) {
        cameraInfoMapper.updateDeviceSerial(originalSerial, newSerial);
    }

    public int remove(Long id) {
        return cameraInfoMapper.updateDelFlag(id);
    }

    public CameraInfo queryById(Long id) {
        return cameraInfoMapper.selectByPrimaryKey(id);
    }
}
